﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuraQR.Bean;
using System.Collections;
using SuraQR.Controller;
using System.Diagnostics;

namespace SuraQR
{
    public partial class OrderDetailFrm : Form
    {
        private Order addNewOrder;
        private bool isEdit = false;
        //private ArrayList menuList;
        private MenuOption seletedMenu = null;
        private ArrayList allMenus;

       
        public OrderDetailFrm()
        {
            InitializeComponent();
        }

        private void btnOrderDetailOk_Click(object sender, EventArgs e)
        {
            this.Close();

            this.checkAutoCalc();
            addNewOrder = (Order)orderMapping.List[0];
            
        }

        private void OrderDetailFrm_Load(object sender, EventArgs e)
        {
            if (isEdit)
            {
                orderMapping.Add(this.AddNewOrder);
            }
            else
            {
                orderMapping.AddNew();
            }
            addNewOrder = null;

            if (ServerRuntimeSingleton.Instance.MenuList == null)
            {
                // load menu
                XmlToMenuService menu = new XmlToMenuService();
                menu.read();
                ServerRuntimeSingleton.Instance.MenuList = menu;
            }
            allMenus = ServerRuntimeSingleton.Instance.MenuList.MenuList;


            foreach (MenuOption m in allMenus)
            {
                cbb_menulist.Items.Add(m.Type);
            }


        }

        internal Order AddNewOrder
        {
            get { return addNewOrder; }
            set { addNewOrder = value; }
        }
        public bool IsEdit
        {
            get { return isEdit; }
            set { isEdit = value; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Order o  = (Order)orderMapping.List[0];
            //4.5           
             o.TaxPerLitter = o.BottlePerLitter * o.AmountBottle;

            //calc
            //step1 check Input
            // input ราคาสุดท้าย  ไม่มีเงือนไข
             if( !isNotEmpty(seletedMenu) ){
                 MessageBox.Show("ไม่ได้เลือก ชนิดสุรา");
                 return;
             }

            //บอกราคา แต่ไม่มีเงือนไข
             if (seletedMenu.TellPriceFlage1 == "TRUE" && seletedMenu.Condition != "TRUE")
             {
                 o.AdValoremTax = (o.WholesalePrice1 * seletedMenu.AdValoremTax) / 100;
             }

             // ราคาตามประกาศ แต่ไม่มีเงือนไข
             if (seletedMenu.GOVPriceFlage1 == "TRUE" && seletedMenu.Condition != "TRUE")
             {
                 o.WholesalePrice1 = Convert.ToDouble( seletedMenu.Param );
           
             }
             // แจ้งราคา แบบมี เงือนไข 
             if (seletedMenu.TellPriceFlage1 == "TRUE" && seletedMenu.Condition == "TRUE")
             {
                 var tmpprice = o.WholesalePrice1;
                 ArrayList condition = new ArrayList();

                 foreach (MenuOption m in allMenus)
                 {
                     if (m.Type == seletedMenu.Type)
                     {
                         condition.Add(m);
                     }
                 }
                 // 1 ราคามาเเจ้ง ตามกำหนด
                 if (seletedMenu.Param.IndexOf("X") != -1)
                 {
                     for (int i = 0; i < condition.Count; i++ )
                     {
                         MenuOption m = (MenuOption)condition[i];
                         if (m.Param.IndexOf("X_LT_") != -1)
                         {
                             string s = m.Param;
                             bool result = tmpprice <= Convert.ToDouble(s.Replace("X_LT_", ""));

                             if (result)
                             {
                                 seletedMenu = m;
                                 Debug.WriteLine("result X_LT_ : " + result);
                                 
                             }

                             continue;
                         }
                         if (m.Param.IndexOf("X_GT_") != -1)
                         {
                             string s = m.Param;
                             bool result2 = tmpprice > Convert.ToDouble(s.Replace("X_GT_", ""));
                             if (result2)
                             {
                                 seletedMenu = m;
                                 Debug.WriteLine("result X_GT_ : " + result2);
                                
                             }

                             continue;

                         }
                     }
                 }
             }

             //1   ภาษีตามมูลค่า
             if (seletedMenu.GOVPriceFlage1 == "TRUE")
             {
                 o.AdValoremTax = (o.WholesalePrice1 * seletedMenu.AdValoremTax * o.BottlePerLitter)  / 100;
             }
             else
             {
                 o.AdValoremTax = (o.WholesalePrice1 * seletedMenu.AdValoremTax) / 100;
             }

             //3 degree เกิน
             double tmpDegree = 0;
             if (o.AlcohorDegree > seletedMenu.Overloadegree)
             {
                 var over = o.AlcohorDegree - seletedMenu.Overloadegree;
                 o.OverLoadDegreeTax = over * o.BottlePerLitter * seletedMenu.OverloadegreeTax;
                 Debug.WriteLine("3 : " + o.OverLoadDegreeTax);

                 // คิดแบบไม่เกิน
                 tmpDegree = o.AlcohorDegree;
                 o.AlcohorDegree = o.AlcohorDegree - over;
             }


             //2 1 ปริมาตรต่อลิตร
             var tmp1 = (o.BottlePerLitter * o.AlcohorDegree * seletedMenu.QuantityTax1) / 100;
             Debug.WriteLine("2.1 : " + tmp1);
             //2 2 ปริมาตรต่อลิตร
             var tmp2 = (o.BottlePerLitter * seletedMenu.QuantityTax2);
             Debug.WriteLine("2.2 : " + tmp2);
             o.QuantityTax = (tmp1 > tmp2) ? tmp1 : tmp2;

             if (tmpDegree != 0)
             {
                 o.AlcohorDegree = tmpDegree;
             }
     

            //4 รวมภาษี 
             o.AmountQuantityTax = o.QuantityTax + o.OverLoadDegreeTax;
            // 4.4 * 5.1
             o.TotalAdValoremTax = o.AmountBottle * o.AdValoremTax;

            // 4.4 * 5.4
             o.TotalQuantityTax = o.AmountBottle * o.AmountQuantityTax;


             orderMapping.List.Clear();
             orderMapping.List.Add(o);
        }

        private bool isNotEmpty( object input ){
            if (input == null) return false;
            return true;
        }

        private void cbb_menulist_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox c = (ComboBox) sender;
            string s = (string)c.SelectedItem;
            foreach (MenuOption m in allMenus)
            {
                if (m.Type == s)
                {
                    seletedMenu = m;

                    // autoCal
                    this.autoCalc(sender,e);
                }
            }
        }

        private void autoCalc(object sender, EventArgs e)
        {
            Debug.WriteLine("change");
            this.checkAutoCalc();
        }

        private void checkAutoCalc()
        {
            if (orderMapping.Count == 0) return;

            Order o = (Order)orderMapping.List[0];
            bool canAutoCal = false;
            if (isNotEmpty(seletedMenu) && o.AlcohorDegree != 0 && o.BottlePerLitter != 0 && o.AmountBottle != 0 )
            {
                
                if (o.WholesalePrice1 == 0)
                {
                    if (seletedMenu.GOVPriceFlage1 == "TRUE")
                    {
                        canAutoCal = true;
                    }
                }
                else
                {
                    canAutoCal = true;
                }
                
            }

            if (canAutoCal)
            {
                this.button1_Click(null, null);
            }
        }

    }
}
